package org.theonept.travian.t4a;

import java.sql.SQLException;

import org.theonept.travian.dao.CompteDAO;
import org.theonept.travian.dao.DatabaseHelper;
import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Message;
import org.theonept.travian.t4a.account.CompteActivity;
import org.theonept.travian.t4a.account.ListCompteAdapter;
import org.theonept.travian.t4a.account.LoginTravianActivity;
import org.theonept.travian.t4a.pref.SettingsActivity;
import org.theonept.travian.t4a.service.Travian4AndoidService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
	ListView listCompte;
	// ImageButton buttonAddAccount;
	private ListCompteAdapter compteAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DatabaseHelper.getInstance(getApplicationContext());
		Intent intentT4A = new Intent(getApplicationContext(), Travian4AndoidService.class);
		setContentView(R.layout.activity_main);
		if (getIntent().getExtras() != null) {
			String mode = getIntent().getExtras().getString("mode");
			if (mode != null) {
				if (mode.equals("message")) {
					Long idcompte = getIntent().getExtras().getLong("compte_id");
					Compte compte = null;
					try {
						compte = CompteDAO.getINSTANCE().queryForId(idcompte);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Message message = (Message) getIntent().getExtras().getSerializable("message");

					Intent intent = new Intent(this, CompteActivity.class);
					intent.putExtra("compte", compte);
					intent.putExtra("mode", mode);
					if (message != null) {
						intent.putExtra("message", message);
					}
					startActivity(intent);
				}
			}
		}

		getApplicationContext().startService(intentT4A);

		listCompte = (ListView) findViewById(R.id.listCompte);
		listCompte.setOnItemClickListener(this);
		compteAdapter = new ListCompteAdapter(this, CompteDAO.getINSTANCE().queryForAll());
		listCompte.setAdapter(compteAdapter);
		getActionBar();
		// buttonAddAccount = (ImageButton) findViewById(R.id.buttonAddAccount);
		// buttonAddAccount.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		compteAdapter.clear();
		compteAdapter.addAll(CompteDAO.getINSTANCE().queryForAll());
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (isNetworkAvailable()) {
			Compte compte = compteAdapter.getItem(position);
			Intent intent = new Intent(this, CompteActivity.class);
			intent.putExtra("compte", compte);

			startActivity(intent);
		} else {
			Toast.makeText(getApplicationContext(), "Make Sure that an internet connection is available",
					Toast.LENGTH_LONG).show();
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_add_account:
			Intent intent = new Intent(this, LoginTravianActivity.class);
			startActivity(intent);
			break;
		case R.id.action_settings:
			Intent intentSet = new Intent(this, SettingsActivity.class);
			startActivity(intentSet);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	}
}
